import axios from 'axios'

const instance = axios.create({
  baseURL: 'http://localhost:9000',
  timeout: 15000
})

instance.interceptors.request.use((config) => {
  try {
    const stored = localStorage.getItem('iot_frontend_auth')
    if (stored) {
      const { token } = JSON.parse(stored)
      if (token) {
        config.headers = config.headers || {}
        // 与后端实现对齐：Authorization 直接传 token（后端未去掉 Bearer 前缀）
        config.headers.Authorization = token
        config.headers.token = token
        config.headers['X-Token'] = token
      }
    }
  } catch (_) { }
  return config
})

instance.interceptors.response.use(
  (res) => res,
  (error) => {
    return Promise.reject(error)
  }
)

export default instance


